Xbasic

SQL::ResultSetToCSVFile Method

Syntax

dim result as C = ToCSVFile(TargetFile as C [ IncludeHeaderRow as L = .f. [, RowsToCopy as N = -1 [, StartRow as N = -1 [, UpperCaseNames as L = .f. [, DateTimeFormat as C = "" [, DateFormat as C = "" [, TextToSubstituteForBinary as C]]]]]]])

Arguments

TargetFileCharacter

The name of the file to store the data, including the file path.

IncludeHeaderRowLogical

Default = .f.. If true, the first row in the result will be the column names.

RowsToCopyNumeric

Default = -1 (copy all rows). The number of rows to copy.

StartRowNumeric

Default = -1 (start with next row in the result set). The starting row.

UpperCaseNamesLogical

Default = .f.. If true and the header row is included, the header row column names will be converted to uppercase.

DateTimeFormatCharacter

Default = "". The date time format used to format date time fields in the CSV file. See Date and Time Format Elements for more information about date time formats.

DateFormatCharacter

Default = "". The date format used to format date fields in the CSV file. See Date and Time Format Elements for more information about date formats.

TextToSubstituteForBinaryCharacter

Default = "<Binary Data>". A text string used in place of binary data in the CSV file.

Returns

resultLogical

Returns .T. if the data was successfully saved to a CSV file. If result is .F., the file was not created. Check the result of SQL::ResultSet's callResult object for more info about the error.

Description

Write a ResultSet to a file in Comma Separated Variable (CSV) format.

dim cn as SQL::Connection

' open the connection
if (cn.open("::Name::conn")) then

    ' Query the database
    if (cn.execute("SELECT * FROM Customers") then

        ' Save the query to a CSV file:
        dim filename as c = "C:/customers.csv"
        if (cn.resultSet.toCSVFile(filename)) then
            ui_msg_box("Success!","Customer query written to " + filename)
        else
            ' CSV File creation failed. Check the callResult for more info.
            showvar(cn.resultSet.callResult)
        end if
    else
        ' SELECT failed. Check callResult for more info.
        showvar(cn.callResult)
    end if
    cn.close()
else
    ' Unable to establish connection. Check callResult for more info.
    showvar(cn.callResult)
end if